/**
 * Created by lixin on 2019-09-12
 */
'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
    async index() {
        const {ctx} = this;
        console.log('------user')
        /*console.log(app);
        console.log(ctx);*/
        // post 接收参数
        console.log(ctx.request.body);
        console.log('------')
        // 校验参数
        const createRule = {
            id: {type: 'number', max: 10},
        };
        // 校验参数
        try {
            ctx.validate(createRule);
        } catch (err) {
            ctx.logger.warn(err.errors);
            ctx.body = ctx.body = {
                code: '0001',
                msg: err.errors[0],
                data: {}
            };
            return;
        }


        const _req = ctx.request.body;
        if (_req.id && _req.id === 9) {
            ctx.body = {
                code: '0000',
                msg: '成功-post',
                data: {
                    pageNo: 1,
                    pageSize: 20,
                    total: 100,
                    datas: [
                        {
                            id: '1234',
                            name: 'lx',
                        }
                    ]
                }
            };

        } else {
            ctx.body = {
                code: '0001',
                msg: '参数错误',
                data: {}
            }
        }


    }

    async userMessage() {
        const {ctx} = this;
        const user = await ctx.service.user.queryUserMessage(1);
        if (user) {
            ctx.body = {
                code: '0000',
                msg: '成功',
                data: [user]
            };
        } else {
            ctx.body = {
                code: '0002',
                msg: '请求用户信息失败',
                data: []
            };
        }

    }
}

module.exports = UserController;
